﻿using Niti.OrderManage.OrderManage.Enum;
using Niti.OrderManage.OrderManage.ValueObject;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace Niti.OrderManage.OrderManage
{
   public  class CheckOutOrder : Entity<int>
    {
        /// <summary>
        /// 出库订单编号
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 申请人
        /// </summary>
        public string ApplyUser { get; set; }
        /// <summary>
        /// 领料说明
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 申请时间
        /// </summary>
        public DateTime CreationTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? CheckTime { get; set; }
        /// <summary>
        /// 草稿状态 
        /// </summary>
        public OrderStatusCode IsDraft { get; set; } = OrderStatusCode.Draft;
        /// <summary>
        /// 订单商品种类的数量
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 订单商品总价
        /// </summary>
        public float TotalPrice { get; set; }
        /// <summary>
        /// 订单明细
        /// </summary>
        [NotMapped]
        public virtual List<CheckOutOrderLineItem> CheckOutOrderLineItem { get; set; } = new List<CheckOutOrderLineItem>();

        protected CheckOutOrder()
        {

        }

        public CheckOutOrder(string code, string user,string desc)
        {
            Check.NotNull(code, nameof(code));
            Code = code;
            ApplyUser = user;
            Description = desc;
        }



        //根据整个订单添加或者修改明细
        public void AddOrChangeItem(string orderCode, OrderLineItem_Commodity commodity, int count,bool isFromUpdate=false)
        {
            //草稿状态
            if (IsDraft == OrderStatusCode.Draft)
            {
                if (count <= 0)
                {
                    throw new ArgumentException(
                        "You can not add zero or negative count of commodity!",
                        nameof(count)
                    );
                }
               
                var existingLine = CheckOutOrderLineItem.FirstOrDefault(ol => ol.Commodity_Code == commodity.Code);

                //更新时候调用清空
                if (isFromUpdate == true)
                {
                    Count = CheckOutOrderLineItem.Count;
                }
                if (existingLine == null)
                {
                    CheckOutOrderLineItem.Add(new CheckOutOrderLineItem(orderCode, CheckOutOrderLineItem.Count + 1, commodity, count));
                    Count += 1;
                }
                else
                {
                    existingLine.ChangeSubtotal(commodity, count);
                }


                TotalPrice = 0;//清0
                foreach (var item in CheckOutOrderLineItem)
                {
                    TotalPrice += item.Subtotal;
                }


            }

        }
        //根据整个订单删除明细
        public void RemoveItem(int lineNumber, OrderLineItem_Commodity commodity)
        {
            //草稿状态
            if (IsDraft == OrderStatusCode.Draft)
            {
                var existingLine = CheckOutOrderLineItem == null ? null : CheckOutOrderLineItem.FirstOrDefault(ol => ol.LineNumber == lineNumber && ol.Commodity_Code == commodity.Code);

                if (existingLine == null)
                {
                    throw new ArgumentException(
                         "You can not remove this commodity,because it is zero or null!"
                     );
                }
                else
                {
                    CheckOutOrderLineItem.Remove(existingLine);
                }

                Count -= 1;

                foreach (var item in CheckOutOrderLineItem)
                {
                    TotalPrice += item.Subtotal;
                }


            }

        }

        //单独追加一条明细
        public bool AddItem(string orderCode, OrderLineItem_Commodity commodity, int count, List<CheckOutOrderLineItem> orderLineItem)
        {
            bool isInsertOrUpdate = false;
            //草稿状态
            if (IsDraft == OrderStatusCode.Draft)
            {
                CheckOutOrderLineItem = orderLineItem;
                var existingLine = CheckOutOrderLineItem.FirstOrDefault(ol => ol.Commodity_Code == commodity.Code);


                if (existingLine == null)
                {

                    CheckOutOrderLineItem.Add(new CheckOutOrderLineItem(orderCode, CheckOutOrderLineItem.OrderByDescending(x => x.LineNumber).FirstOrDefault().LineNumber + 1, commodity, count));
                    Count += 1;
                    isInsertOrUpdate = true;//返回新增
                }
                else
                {
                    existingLine.ChangeSubtotal(commodity, count, true);
                    isInsertOrUpdate = false;//返回修改
                }


                TotalPrice = 0;
                foreach (var item in CheckOutOrderLineItem)
                {
                    TotalPrice += item.Subtotal;
                }


            }
            return isInsertOrUpdate;

        }

        //单独修改一条明细
        public void ChangeItem(string orderCode, OrderLineItem_Commodity commodity, int count, List<CheckOutOrderLineItem> orderLineItem)
        {
            //草稿状态
            if (IsDraft == OrderStatusCode.Draft)
            {
                CheckOutOrderLineItem = orderLineItem;
                var existingLine = CheckOutOrderLineItem.FirstOrDefault(ol => ol.Commodity_Code == commodity.Code);


                if (existingLine == null)
                {
                    throw new ArgumentException(
                         "There is not exist this commodity!"
                     );
                }
                else
                {
                    existingLine.UpdateTime = DateTime.Now;
                    existingLine.ChangeSubtotal(commodity, count);
                }


                TotalPrice = 0;
                foreach (var item in CheckOutOrderLineItem)
                {
                    TotalPrice += item.Subtotal;
                }


            }
        }

        //单独删除一条明细
        public void DeleteItem(string orderCode, List<CheckOutOrderLineItem> orderLineItem)
        {
            //草稿状态
            if (IsDraft == OrderStatusCode.Draft)
            {
                CheckOutOrderLineItem = orderLineItem;

                Count = orderLineItem.Count;
                TotalPrice = 0;
                foreach (var item in CheckOutOrderLineItem)
                {
                    TotalPrice += item.Subtotal;
                }


            }
        }


    }
}
